/*
 * @lc app=leetcode.cn id=1779 lang=typescript
 *
 * [1779] 找到最近的有相同 X 或 Y 坐标的点
 */

// @lc code=start
function nearestValidPoint(x: number, y: number, points: number[][]): number {
    let min = Infinity;
    let minIndex = -1;
    for (let i = 0; i < points.length; i++) {
        const [X, Y] = points[i];
        if (X === x || y === Y) {
            const dist = Math.abs(x - X) + Math.abs(y - Y);
            if (dist < min) {
                minIndex = i;
                min = dist;
            }
        }
    }

    return minIndex;
}
// @lc code=end
